package com.ellricky.teacher.ui.mycourse.course

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.http.callback.EListCallback
import com.ellricky.baselib.ui.base.BaseRefreshFragment
import com.ellricky.teacher.*

class CourseFragment : BaseRefreshFragment<Void, BasePresenter<Void>, MyCourseEntity>() {

    private var pageType = -1
    private var weekDate: String? = null

    companion object {
        fun newInstance(pageType: Int): CourseFragment {
            val fragment = CourseFragment()
            val bundle = Bundle()
            bundle.putInt("pageType", pageType)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initUIView(view: View?) {
        pageType = arguments?.getInt("pageType") ?: -1
        super.initUIView(view)
        hideToolBar()
    }

    override fun initParamMap() {
        super.initParamMap()
        if (pageType == FRAGMENT_COURSE_VIP) {
            paramMap["weekDate"] = weekDate
        }
    }


    override fun getResponseCallback(): EListCallback<MyCourseEntity> {
        return object : EListCallback<MyCourseEntity>(mContext) {
            override fun onSuccess(data: MutableList<MyCourseEntity>?, msg: String?) {
                dataSource = data
                rvView.onLoadFinish(dataSource, refresh)
            }

            override fun onErrorCode(code: String?, msg: String?) {
                super.onErrorCode(code, msg)
                rvView.onRefreshFailed(refresh)
            }

            override fun onHttpError(code: String?, msg: String?) {
                super.onHttpError(code, msg)
                rvView.onRefreshFailed(refresh)
            }
        }
    }

    override fun getRvAdapter(): BaseRecyclerAdapter<MyCourseEntity> {
        return MyCourseAdapter(mContext, pageType)
    }

    override fun getUrlPath(): String {
        return when (pageType) {
            FRAGMENT_COURSE_EXPERIENCE -> API_COURSE_TY_LIST
            FRAGMENT_COURSE_TEMP -> API_COURSE_TEMP_LIST
            FRAGMENT_COURSE_VIP -> API_COURSE_VIP_LIST
            else -> ""
        }
    }


    override fun onItemClick(adapterView: AdapterView<*>?, view: View?, i: Int, l: Long) {
        super.onItemClick(adapterView, view, i, l)
        val entity = adapter[i]
        jumpMyCourseItem(null, this, entity.attend, entity.id,entity.preId)
    }


    fun setWeekDate(weekDate: String?) {
        this.weekDate = weekDate
    }


    fun postRequest() {
        loadData(true)
    }

}
